<?php
class AjaxuploadHelper extends AppHelper { 

     var $helpers = array('Javascript', 'Ajax', 'Html');
     /*
     function _wrapper($id, $script) { 
        $this->Javascript->link('ajaxupload.js', false); 
        $scr = implode(',', $script); 
        $script = "document.observe(\"dom:loaded\", function() {new Ajax_upload('$id',{{$scr}});});"; 
        return $this->Javascript->codeBlock($script); 
     }
     
     function _options($options = null) { 
         $return = array(); 
         if (!empty($options['data'])) { 
             $data = $options['data']; 
            $ret = "data: {"; 
             foreach ($data as $key=>$value) { 
                 $retdata = "$key : $value"; 
            } 
            $ret .= $retdata."} "; 
            $return[] = $ret; 
         } 
         if (!empty($options['autoSubmit'])) { 
             $return[] = "autoSubmit: {$options['autoSubmit']}"; 
         } 
         if (!empty($options['responseType'])) { 
             $return[] = "responseType: {$options['responseType']}"; 
        } 
        return $return; 
    } 
    
    function _url($url) { 
         return "action: \"".Router::url($url)."\""; 
    } 
    
    function _name($url) { 
        $name = Inflector::classify($url['controller']); 
        return "name: \"data[$name][File]\""; 
    } 
    
    function _id($id) { 
        return "$('$id')"; 
    }
    
     function _submit($id, $options) { 
        $submit = ""; 
        if (!empty($options['files'])) { 
            $files = array(); 
            if (is_array($options['files'])) { 
                foreach ($options['files'] as $type) { 
                    $files[] = $this->__files($type); 
                } 
                $file = implode("|", $files); 
                $type = implode(", ", $options['files']); 
            } else { 
                $file = $this->__files($options['files']); 
                $type = $options['files']; 
            } 
            $submit .= "if (ext && /^($file)$/.test(ext)){ } else { alert('Only $type files allowed'); return false; } "; 
        } 
        if (!empty($options['busy'])) { 
            $submit .= "$('{$options['busy']}').toggle; "; 
        } 
        if (!empty($options['disable'])) { 
            $submit .= "$id.diable; "; 
        } 
        return "onSubmit: function(file, ext){".$submit."}"; 
    } 
    
     function __files($filetype) { 
        switch ($filetype) { 
            case "image": 
                $return = "jpg|png|jpeg|gif"; 
                break; 
            case "text": 
                $return = "txt|html|htm|doc|odt"; 
                break; 
            case "video": 
                $return = "flv"; 
                break; 
            case "music": 
                $return = "mp3"; 
                break; 
            default: 
                $return = ""; 
                break; 
        } 
        return $return; 
    } 
    */
    function _complete($id, $options) { 
        $submit = ""; 
        if (!empty($options['busy'])) { 
            $submit .= "$('{$options['busy']}').toggle; "; 
        } 
        if (!empty($options['disable'])) { 
            $submit .= "$id.enable; "; 
        } 
        if (!empty($options['update'])) { 
            if (is_array($options['update'])) { 
                $type = $options['update']['reply']?'response':'file'; 
                if (!empty($options['update']['element'])) { 
                    $submit .= "$$('#{$options['update']['id']}')[0].insert(new Element('{$options['update']['element']}').update({$type})); "; 
                } else { 
                    $submit .= $this->_id($options['update']['id']).".update({$type}); "; 
                } 
            } 
        } 
        return "onComplete: function(file, response){".$submit."}"; 
    } 

    function upload($button_id, $url, $options) {
		APP::import('Model','Tallere');
		$this->Tallere=new Tallere();
    	$script = $this->_options($options); 
        $script[] = $this->_url($url); 
        $script[] = $this->_name($url); 
        $script[] = $this->_submit($this->_id($button_id), $options); 
        $script[] = $this->_complete($this->_id($button_id), $options); 
        return $this->_wrapper($button_id, $script); 
    }
     
 } 
?> 
    
     
} 